<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="下发状态" prop="f2604">
        <el-select v-model="queryParams.f2604" clearable @keyup.enter.native="handleQuery" placeholder="请选择下发的状态">
          <el-option label="通过" :value="0"></el-option>
          <el-option label="发起" :value="1"></el-option>
          <el-option label="拒绝" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="f2607">
        <el-input v-model="queryParams.f2607" placeholder="请输入用户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门名" prop="f2609">
        <el-input v-model="queryParams.f2609" placeholder="请输入部门名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="T26List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="f2607" />
      <el-table-column label="部门名" align="center" prop="f2609" />
      <el-table-column label="申请toen数量" align="center" prop="f2602" />
      <el-table-column label="实际下发数量" align="center" prop="f2603" />
      <el-table-column label="下发状态" align="center" prop="f2604">
        <template slot-scope="scope">
          <el-tag :type="getTagType(scope.row.f2604)">
            {{ getStatusText(scope.row.f2604) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="F2605" width="180" />
      <el-table-column label="下发时间" align="center" prop="F2606" width="180" />
      <el-table-column label="申请原因" align="center" prop="f2611" />
      <!-- 操作列 -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 仅当 f2604 为 1 时显示操作按钮 -->
          <div v-if="scope.row.f2604 === 1">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">审批</el-button>
          </div>
          <div v-else>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改token申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户名" prop="f2607">
          <el-input disabled v-model="form.f2607" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="部门名" prop="f2609">
          <el-input disabled v-model="form.f2609" placeholder="请输入部门名" />
        </el-form-item>
        <el-form-item label="申请toen数量" prop="f2602">
          <el-input disabled v-model="form.f2602" placeholder="请输入申请使用的toen数量" />
        </el-form-item>
        <el-form-item label="实际下发数量" prop="f2603">
          <el-input :disabled="form.f2604 !== 1" v-model="form.f2603" placeholder="请输入实际下发的数量" />
        </el-form-item>
        <el-form-item label="下发状态" prop="f2604">
          <el-select disabled style="width: 100%;" v-model="form.f2604" clearable placeholder="请选择下发的状态">
            <el-option label="通过" :value="0"></el-option>
            <el-option label="发起" :value="1"></el-option>
            <el-option label="拒绝" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="f2605">
          <el-date-picker disabled style="width: 100%;" clearable v-model="form.f2605" type="date"
            value-format="yyyy-MM-dd" placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请原因">
          <el-input disabled v-model="form.f2611" />
        </el-form-item>
      </el-form>
      <div v-if="form.f2604 === 1" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="rejectSubmit">拒 绝</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listT26, getT26, auditT26, auditT26Fail } from "@/api/system/T26";

export default {
  name: "T26",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // token申请表格数据
      T26List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 申请原因时间范围
      daterangeF2605: [],
      // 申请原因时间范围
      daterangeF2606: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        f0601: null,
        f2602: null,
        f2603: null,
        f2604: 1,
        f2605: null,
        f2606: null,
        f0301: null,
        f0501: null,
        f2607: null,
        f2608: null,
        f2609: null,
        f1801: null,
        f2610: null,
        f2611: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        f2603: [
          { required: true, message: "请输入申请使用的toen数量", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询token申请列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeF2605 && '' != this.daterangeF2605) {
        this.queryParams.params["beginF2605"] = this.daterangeF2605[0];
        this.queryParams.params["endF2605"] = this.daterangeF2605[1];
      }
      if (null != this.daterangeF2606 && '' != this.daterangeF2606) {
        this.queryParams.params["beginF2606"] = this.daterangeF2606[0];
        this.queryParams.params["endF2606"] = this.daterangeF2606[1];
      }
      listT26(this.queryParams).then(response => {
        this.T26List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 拒绝按钮
    rejectSubmit() {
      auditT26Fail(this.form).then(response => {
        this.$modal.msgSuccess("审批成功");
        this.open = false;
        this.getList();
      });
    },
    // 表单重置
    reset() {
      this.form = {
        f2601: null,
        f0601: null,
        f2602: null,
        f2603: null,
        f2604: null,
        f2605: null,
        f2606: null,
        f0301: null,
        f0501: null,
        f2607: null,
        f2608: null,
        f2609: null,
        f1801: null,
        f2610: null,
        f2611: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeF2605 = [];
      this.daterangeF2606 = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.f2601)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加token申请";
    },
    /** 审批按钮操作 */
    handleUpdate(row) {
      this.reset();
      const F2601 = row.f2601 || this.ids
      getT26(F2601).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审批token申请";
      });
    },
    /** 查看按钮 */
    handleCheck(row) {
      this.reset();
      const F2601 = row.f2601 || this.ids
      getT26(F2601).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "token申请详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        auditT26(this.form).then(res => {
            this.$modal.msgSuccess("审批成功");
            this.open = false;
            this.getList();
        }).catch(err => {

        }
        )
      });
    },
    getStatusText(status) {
      // 根据状态值返回对应的文字
      if (status === 0) {
        return "通过";
      } else if (status === 1) {
        return "发起";
      } else if (status === 2) {
        return "拒绝";
      }
      return "";
    },
    getTagType(status) {
      // 根据状态值返回对应的标签样式
      if (status === 0) {
        return "success";
      } else if (status === 1) {
        return "warning";
      } else if (status === 2) {
        return "danger";
      }
      return "";
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/T26/export', {
        ...this.queryParams
      }, `T26_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
